//
//  GWQuestionnaireCellTextField.m
//
//  Created by Grzegorz Wójcik on 10.03.2014.
//
//

#import "GWQuestionnaireCellTextField.h"
@interface GWQuestionnaireCellTextField ()
{
    GWQuestionnaire *owner;
    int row;
}
@end
@implementation GWQuestionnaireCellTextField

- (void)awakeFromNib
{
    self.textView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.textView.layer.borderWidth = 1;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)keyboardWillHide:(NSNotification *)notification
{
    if (self.textView.isEditable) {
        [_textView resignFirstResponder];
    }
}

-(void)setContent:(GWQuestionnaireItem*)item row:(int)r
{
    row = r;
    [self.textView setText:item.userAnswer];
    [self setDefaultText];
}
-(void)setDefaultText
{
    if(!_textView.text || [_textView.text length] == 0)
    {
//        _textView.text = NSLocalizedString(@"填写此次试驾的体验",nil);
    }
}
-(void)setOwner:(GWQuestionnaire*)val
{
    owner = val;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [_textView resignFirstResponder];
}
#pragma mark UITextField delegate
- (void)textViewDidEndEditing:(UITextView *)textView
{
    [owner performSelector:@selector(surveyCellUserAnswerChanged:atIndex:) withObject:self.textView.text withObject:[NSNumber numberWithInt:row]];
    [self setDefaultText];
}

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    self.backTF(textView);
    return YES;
}

-(BOOL)textView:(UITextView *)textView shouldChangetTextInRange:(NSRange)range replacementText:(NSString *)text{
    if([text isEqualToString:@"\n"]){
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}

@end
